(function($, owner) {
	owner.servPath = "https://sunafei.wilddogio.com/"
	//owner.servPath = "http://192.168.1.134:8080/"
	/**
	 * 创建状态
	 */
	owner.createState = function(result) {
		var state = owner.getState();
		state.user = result.data;
		state.jsessionId = result.data.jsessionId;
		state.data = result.data;
		state.token = "token123456789";
		owner.setState(state);
	};

	/**
	 * 获取当前状态
	 **/
	owner.getState = function() {
		var stateText = localStorage.getItem('$state') || "{}";
		return JSON.parse(stateText);
	};

	/**
	 * 设置当前状态
	 **/
	owner.setState = function(state) {
		state = state || {};
		localStorage.setItem('$state', JSON.stringify(state));
		var settings = owner.getSettings();
		settings.gestures = '';
		owner.setSettings(settings);
	};

	/**
	 * 获取应用本地配置
	 **/
	owner.setSettings = function(settings) {
		settings = settings || {};
		localStorage.setItem('$settings', JSON.stringify(settings));
	}

	/**
	 * 设置应用本地配置
	 **/
	owner.getSettings = function() {
		var settingsText = localStorage.getItem('$settings') || "{}";
		return JSON.parse(settingsText);
	}
	/***
	 * 请求服务器
	 * @param {Object} action
	 * @param {Object} param
	 * @param {Object} callback
	 */
	owner.ajax = function(action, param, callback) {
		var data = param || {};
		data.token = owner.getState().token;
		mui.ajax(app.servPath + action,{
			data:data,
			headers: {'Cookie':"JSESSIONID=" + owner.getState().jsessionId},
			dataType:'json',//服务器返回json格式数据
			type:'post',//HTTP请求类型
			timeout:10000,//超时时间设置为10秒；
			crossDomain: true,
    		xhrFields: {withCredentials: true},
			success:function(result){
				if(result.state == "error"){
					plus.nativeUI.toast(result.message);
				}
				if(result.state == 'success'){
					callback(result.data);
				}
			},
			error:function(xhr,type,errorThrown){
				plus.nativeUI.toast("无法连接服务器，请检查网络！");
			}
		});
	};
}(mui, window.app = {}));